﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Collections;

namespace Trig_AMJ_IT_Customforms.Layouts.Trig_AMJ_IT_Forms
{
    public partial class IT_Helpdesk_Print : LayoutsPageBase
    {

        SPHelper obj = new SPHelper();

        #region "Properties"

        string CurrentUser = SPContext.Current.Web.CurrentUser.LoginName;

        public static string SiteURL;
        public static string TicketNo;
        public static string MainLstName;
        public static string TaskLstName;
        public static string AttLibName;
        public static string CmntsLstName;
        public static string WorkflowConfigList;
        public static string lstGuid { get; set; }
        public string camlQry { get; set; }
        public static string Mode { get; set; }

        string ErrorMsgs;

        public static string DocumentTypesList { get; set; }
        string WFCurrentOrder = string.Empty;
        string WFTaskType = string.Empty;

        Demand_Voucher_FormClass demandVoucherclass = new Demand_Voucher_FormClass(); // Helper class for this solution
        Trig_WF trigWFClass = new Trig_WF();

        string[] buttonVisibilty = new string[5];

        Label EmpName; Label EmpID; Label Division; Label Designation; Label MobNo;

        #endregion

        /// <summary>
        /// Constructor to store List names
        /// </summary>

        public IT_Helpdesk_Print()
        {
            SiteURL = SPContext.Current.Site.Url;
            SiteURL = SiteURL + "/sites/forms/";
            MainLstName = "IT Helpdesk Forms";
            TaskLstName = "IT Helpdesk Tasks";
            CmntsLstName = "IT Helpdesk Comments";
            AttLibName = "Attachments";
            WorkflowConfigList = "Workflow Configuration List";
            DocumentTypesList = "DocumentTypes";
        }

        #region "User control manipulation"
        /// <summary>
        /// Get user control controls using find control method 
        /// </summary>
        private void GetUserControls()
        {
            EmpName = userInfo.FindControl("lblEmpName") as Label;
            EmpID = userInfo.FindControl("lblEmpID") as Label;
            Division = userInfo.FindControl("lblDivision") as Label;
            Designation = userInfo.FindControl("lblDesignation") as Label;
            MobNo = userInfo.FindControl("lblMobNo") as Label;
        }

        #endregion

        #region "Page Load"

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                
                Mode = Request.QueryString["Mode"];
                GetUserControls();

                if (!IsPostBack)
                {
                   
                    if (Mode == "Print" || Mode == "View")
                    {
                        ReLoadDetails(); // Bind data from Master List
                    }
                    else
                    {
                        pnlFullForm.Visible = false;
                        pnlAccessDenied.Visible = true;
                        lblAccessDeniedMsg.Visible = true;
                        lblAccessDeniedMsg.Text = "Unable to open the form. Value is missing. Contact administrator";
                    }
                }

            }
            catch (Exception ex)
            {
                ErrorMsgs = "Trig_AMJ_IT_Forms :: IT Helpdesk Form" + " :: " + "Parent" + " :: " + "Page_Load";
                obj.GenerateExceptionLog(ErrorMsgs, ex.Message, "");
            }

        }

        #endregion
        
        /// <summary>
        /// Function to bind data after the Form gets submitted.
        /// </summary>
        /// <param name="SiteURL"></param>

        #region "Reload Forms"

        public void ReLoadDetails()
        {
            try
            {
                string RefNo = Convert.ToString(Request.Params["RefID"]);

                camlQry = "<Where><Eq><FieldRef Name=\"Reference_x0020_Number\" /><Value Type=\"Text\">" + RefNo + "</Value></Eq></Where>";
                SPListItem lstDatas = obj.GetSPListItem(SiteURL, MainLstName, camlQry);
                if (lstDatas != null)
                {
                    lblTxkNo.Text = Convert.ToString(lstDatas["Reference Number"]);
                    dtpDate.Text  = Convert.ToDateTime(lstDatas["RequestDate"]).ToString("dd/MM/yyyy");
                    lblReqCategory.Text = Convert.ToString(lstDatas["RequestCategories"]);
                    lblReqPriority.Text  = Convert.ToString(lstDatas["RequestPriority"]);

                    Label lblFormID = userInfo.FindControl("lblFormID") as Label;
                    lblFormID.Text = lstDatas.ParentList.ID.ToString();

                    if (Convert.ToString(lstDatas["OnBehalfof"]) == "Yes")
                    {
                        lblOnBehalf.Text = "On Behalf of";
                        pnlReqEmpInf.Visible = true;
                        SetValuestoOnBehalfof(lstDatas);
                    }

                    else
                    {
                        pnlReqEmpInf.Visible = false;
                        lblOnBehalf.Text = "Loggedin User";
                    }
                }

                // Get Approved users and bind in Approvers gridview 
                obj.GetApprovedUsers(RefNo, SiteURL, TaskLstName, gvApprovers);
            }
            catch (Exception ex)
            {
                ErrorMsgs = "Trig_AMJ_IT_Forms" + " :: " + "Parent" + " :: " + "ReLoadDetails";
                obj.GenerateExceptionLog(ErrorMsgs, ex.Message, "");
            }
        }


        /// <summary>
        /// Set On behalf of controls value while the form gets submitted by On Behalf of option
        /// </summary>
        /// <param name="lstDatas"></param>
        private void SetValuestoOnBehalfof(SPListItem lstDatas)
        {
            SPFieldUserValue user = new SPFieldUserValue(SPContext.Current.Web, lstDatas["SubmittedFor"].ToString());

            txtReqUser.Text = user.LookupValue;
            txtReqUserID.Text = Convert.ToString(lstDatas["EmployeeID"]);
            txtReqUserDivision.Text = Convert.ToString(lstDatas["Division"]);
            txtReqUserDept.Text = Convert.ToString(lstDatas["Department"]);
            txtReqUserPosCategory.Text = Convert.ToString(lstDatas["Designation"]);
            txtReqUserMobNo.Text = Convert.ToString(lstDatas["MobileNo"]);
            txtReqUserMobNo.Enabled = false;
        }

        
        #endregion
    }
}
